// component/scp-component.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    countData:{
      type:Object,
      value:{},
      observer:function(newval){
        if (JSON.stringify(newval) !== '{}') {
          // console.log(newval);
          var Info = wx.getStorageSync('userInfo')
          this.setData({
          countdata:newval,
          isPraised:newval.praises.some(item=>item === Info.userId),
          isStored:newval.stores.some(item=>item === Info.userId),
          articleId:newval.articleId
        })
        // console.log(this.data);
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    countdata:{},
    articleId:'',
    isStored:false,
    isPraised:false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    praise(){
      let res = wx.getStorageSync('userInfo')
      var data = {articleId:this.data.articleId,praiseId:res.userId}
      if (this.data.isPraised == true) {
        data.code =0
      }
      wx.cloud.callFunction({
        name:'praiseArticle',
        data:data
      })
        .then(res=>{
          this.setData({
            countdata:res.result.data,
            isPraised:!this.data.isPraised
          })
        })
    },
    store(){ 
      let res = wx.getStorageSync('userInfo')
      var data = {articleId:this.data.articleId,storeId:res.userId}
      if (this.data.isStored == true) {
        data.code =0
    }
    wx.cloud.callFunction({
      name:'storeArticle',
      data:data
    })
    .then(res=>{
      this.setData({
        countdata:res.result.data,
        isStored:!this.data.isStored
      })
    })
    }
  }
})
